package guiscreens;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.RowSpec;

import dao.ExecuteQuery;

/**
 * This class handles loading the main window with navigational buttons.
 * 
 * @author Lee
 * @version 1.0
 */
public class MainWindow
{

	private JPanel contentPane;
	private CardLayout c1;
	private JPanel basePanel;
	private JFrame thisFrame;
	public static final int XCENTER = Toolkit.getDefaultToolkit().getScreenSize().width/2;
	public static final int YCENTER = Toolkit.getDefaultToolkit().getScreenSize().height/2;

	/**
	 * Create the frame.
	 */
	public MainWindow() 
	{
		thisFrame = new JFrame();
		thisFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		thisFrame.setBounds(XCENTER - 275, YCENTER - 300, 550, 600);
		thisFrame.setTitle("Welcome!");
		thisFrame.setVisible(true);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		thisFrame.setContentPane(contentPane);

		basePanel = new JPanel();
		c1 = new CardLayout();
		basePanel.setLayout(c1);
		contentPane.add(basePanel, BorderLayout.CENTER);
		basePanel.setName("BASECARD");

		JPanel panel = new JPanel();
		panel.setBackground(Color.WHITE);
		basePanel.add(panel, BorderLayout.CENTER);
		panel.setLayout(new FormLayout(new ColumnSpec[] {
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,},
				new RowSpec[] {
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("max(23dlu;default)"),
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("max(25dlu;default)"),
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,}));

		JLabel lblWhatWouldYou = new JLabel("What would you like to do?");
		panel.add(lblWhatWouldYou, "4, 4");

		JButton btnPostANew = new JButton("Post a New Item for Auction");
		panel.add(btnPostANew, "4, 8");
		btnPostANew.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent event)
			{
				NewItem newItem = new NewItem(c1, basePanel, thisFrame);
				basePanel.add(newItem, "NEWITEMCARD");
				c1.show(basePanel, "NEWITEMCARD");
			}
		});

		JButton btnSearchForItems = new JButton("Search for Items");
		panel.add(btnSearchForItems, "10, 8");
		btnSearchForItems.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				SearchItem searchItem = new SearchItem(c1, basePanel, thisFrame);
				basePanel.add(searchItem, "SEARCHCARD");
				c1.show(basePanel, "SEARCHCARD");
			}
		});

		JButton btnRetrieveAuctionResults = new JButton("Retrieve Auction Results");
		btnRetrieveAuctionResults.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event) 
			{
				AuctionResults results = new AuctionResults(c1, basePanel, thisFrame);
				basePanel.add(results, "RESULTCARD");
				c1.show(basePanel, "RESULTCARD");
			}
		});
		panel.add(btnRetrieveAuctionResults, "4, 12");

		if (ExecuteQuery.isAdmin(login.username)) 
		{
			String position = ExecuteQuery.getPosition(login.username);
			JLabel lblNewLabel = new JLabel("Position: " + position);
			panel.add(lblNewLabel, "10, 4");
			JButton btnViewCategoryReport = new JButton("View Category Report");
			btnViewCategoryReport.addActionListener(new ActionListener() 
			{
				public void actionPerformed(ActionEvent event)
				{
					ViewCatReport report = new ViewCatReport(c1, basePanel, thisFrame);
					basePanel.add(report, "REPORTCARD");
					c1.show(basePanel, "REPORTCARD");
				}
			});
			panel.add(btnViewCategoryReport, "4, 16");

			JButton btnViewUserReport = new JButton("View User Report");
			btnViewUserReport.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent event)
				{
					UserReportPanel uPanel = new UserReportPanel(c1, basePanel, thisFrame);
					basePanel.add(uPanel, "USERCARD");
					c1.show(basePanel, "USERCARD");
				}
			});
			panel.add(btnViewUserReport, "10, 16");
		}
	}

}
